package org.gbif.metrics.cube.bucketer;

import org.gbif.api.vocabulary.BasisOfRecord;
import org.gbif.occurrencestore.util.BasisOfRecordConverter;

import java.util.List;

import com.google.common.collect.ImmutableList;
import com.urbanairship.datacube.BucketType;
import com.urbanairship.datacube.Bucketer;
import com.urbanairship.datacube.CSerializable;
import com.urbanairship.datacube.serializables.IntSerializable;

/**
 * Bucketer that uses the basis of record converter to serialize as Ints
 */
public class BasisOfRecordBucketer implements Bucketer<BasisOfRecord> {
  // The number of bytes this buckets into
  public static final int BYTES=4;
  
  private static final BasisOfRecordConverter BOR_CONVERTER = new BasisOfRecordConverter();
  
  @Override
  public List<BucketType> getBucketTypes() {
    return ImmutableList.of(BucketType.IDENTITY);
  }
  
  @Override
  public CSerializable bucketForWrite(BasisOfRecord coordinate, BucketType bucketType) {
      return bucketInternal(coordinate, bucketType);
  }

  @Override
  public CSerializable bucketForRead(Object coordinate, BucketType bucketType) {
      return bucketInternal((BasisOfRecord)coordinate, bucketType);
  }  

  public CSerializable bucketInternal(BasisOfRecord c, BucketType bucketType) {
    if (bucketType != BucketType.IDENTITY) {
      throw new IllegalArgumentException("You can only use " + BasisOfRecordBucketer.class.getSimpleName()
        + " with the default identity bucketer");
    }
    return new IntSerializable(BOR_CONVERTER.fromEnum(c));
  }

}
